<?php
/**
 * Created by PhpStorm.
 * User: suncco
 * Date: 2018/8/15
 * Time: 16:33
 */

namespace app\admin\logic;


use think\Model;
use think\Db;

/**
 * 微信信息，菜单修改类
 * Class WechatLogic
 * @package app\admin\logic
 */
class WechatLogic extends Model
{
    /**
     * 更新配置
     * @param string $data
     * @return boolean
     */
    public function update1($data="") {

        $data['wechatname'] = isset($data['wechatname']) ? strip_tags($data['wechatname']) : '';
        $data['token'] = isset($data['token']) ? strip_tags($data['token']) : '';
        $data['appId'] = isset($data['appId']) ? strip_tags($data['appId']) : '';
        $data['AppSecret'] = isset($data['AppSecret']) ? strip_tags($data['AppSecret']) : '';
        $data['encodingAESKey'] = isset($data['encodingAESKey']) ? $data['encodingAESKey'] : '';
        $data['notice'] = isset($data['notice']) ? $data['notice'] : '';
        $Model = db('wechat');

        $Model->where('id=1')->save($data);
        addlog('修改微信配置。');
        return true;
    }

    /**
     * @param string $info
     * @return bool
     */
    public function wxMenuUpdate($info=""){
        $id = $info['id'];
        $data['name'] = isset($info['name']) ? strip_tags($info['name']) : '';
        $data['type'] = isset($info['type']) ? strip_tags($info['type']) : 0;
        $data['key'] = isset($info['key']) ? strip_tags($info['key']) : '';
        $data['url'] = isset($info['url']) ? strip_tags($info['url']) : '';
        $data['parentid'] = isset($info['parentid']) ? strip_tags($info['parentid']) : 0;
        $data['order'] = isset($info['order']) ? strip_tags($info['order']) : '';
        $data['status'] = isset($info['status']) ? strip_tags($info['status']) : '';
        //查询数量是否正确
        if(!$id){
            if($data['parentid'] == 0){
                $data['type'] = 0;

                $count = db('wx_menu')->where("parentid=0")->count();
                if($count>=3){
                    return $message = "一级栏目不能超过3个";
                }
            }else{
                $count = db('wx_menu')->where("parentid=".$data['parentid'])->count();
                if($count>=5){
                    return $message = "二级栏目不能超过5个";
                }
            }
        }


        if ($id) {
            $update = db('wx_menu')->where("menu_id=".$id)->update($data);
            addlog('编辑微信菜单，ID：' . $id);
        } else {
            $update = db('wx_menu')->insert($data);
            addlog('新增微信菜单，名称：' . $data['name']);
        }

        if($update){
            return true;
        }else{
            return false;
        }
    }

    public function wxMenuDel($ids=''){
        $ids = isset($ids) ? $ids : false;
        if (!$ids) {
            return ['flag'=>0,'message'=>'参数错误！'];
        }

        //判断该栏目是否有子栏目
        $son_ids=db('wx_menu')->where('parentid='.$ids)->select();
        if(!empty($son_ids)){//有子栏目，不允许删除
            return ['flag'=>0,'message'=>'有子栏目，不允许删除！！'];
        }

        if (db('wx_menu')->where('menu_id',$ids)->delete()) {

            addlog('删除菜单ID：' . $ids);
            return true;
        } else {
            return false;
        }
    }
}